package com.ruoyi.system.service.impl;

import com.ruoyi.common.core.domain.entity.FoodStock;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.SysNotice;
import com.ruoyi.system.mapper.SysNoticeMapper;
import com.ruoyi.system.service.ISysNoticeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * 公告 服务层实现
 *
 * @author ruoyi
 */
@Service
public class SysNoticeServiceImpl implements ISysNoticeService {
    @Autowired
    private SysNoticeMapper noticeMapper;

    /**
     * 查询公告信息
     *
     * @param noticeId 公告ID
     * @return 公告信息
     */
    @Override
    public SysNotice selectNoticeById(Long noticeId) {
        return noticeMapper.selectNoticeById(noticeId);
    }

    /**
     * 查询公告列表
     *
     * @param notice 公告信息
     * @return 公告集合
     */
    @Override
    public List<SysNotice> selectNoticeList(SysNotice notice) {
        return noticeMapper.selectNoticeList(notice);
    }

    /**
     * 新增公告
     *
     * @param notice 公告信息
     * @return 结果
     */
    @Override
    public int insertNotice(SysNotice notice) {
        return noticeMapper.insertNotice(notice);
    }

    /**
     * 修改公告
     *
     * @param notice 公告信息
     * @return 结果
     */
    @Override
    public int updateNotice(SysNotice notice) {
        return noticeMapper.updateNotice(notice);
    }

    /**
     * 删除公告对象
     *
     * @param noticeId 公告ID
     * @return 结果
     */
    @Override
    public int deleteNoticeById(Long noticeId) {
        return noticeMapper.deleteNoticeById(noticeId);
    }

    /**
     * 批量删除公告信息
     *
     * @param noticeIds 需要删除的公告ID
     * @return 结果
     */
    @Override
    public int deleteNoticeByIds(Long[] noticeIds) {
        return noticeMapper.deleteNoticeByIds(noticeIds);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void createNewRecord(FoodStock resource) {
        BigDecimal up = BigDecimal.ZERO;
        BigDecimal down = BigDecimal.ZERO;
        if (resource != null && resource.getUp() != null) {
            up = resource.getUp();
        }
        if (resource != null && resource.getDown() != null) {
            down = resource.getDown();
        }
        if (resource.getAmount().compareTo(up) == 1) {
            SysNotice notice = new SysNotice();
            notice.setNoticeContent(resource.getName() + "的库存" + resource.getAmount() + "已经超出了上限" + up);
            notice.setNoticeTitle("库存超出上限预警");
            notice.setNoticeType("1");
            notice.setStatus("0");
            notice.setCreateTime(new Date());
            notice.setCreateBy(SecurityUtils.getNickName());
            noticeMapper.insertNotice(notice);
        }
        if (down.compareTo(resource.getAmount()) == 1) {
            SysNotice notice = new SysNotice();
            notice.setNoticeContent(resource.getName() + "的库存" + resource.getAmount() + "已经超出了下限" + down);
            notice.setNoticeTitle("库存超出下限预警");
            notice.setNoticeType("1");
            notice.setStatus("0");
            notice.setCreateTime(new Date());
            notice.setCreateBy(SecurityUtils.getNickName());
            noticeMapper.insertNotice(notice);
        }
    }
}
